package com.example.zhangxin.myhuatuob2b.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.zhangxin.myhuatuob2b.R;
import com.example.zhangxin.myhuatuob2b.fragment.home.home.GoodsListFragment;

public class GoodsListFragmentManager extends AppCompatActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {

    private ImageView imageBack;
    private TextView textGoodsTabTitleName;
    private RadioButton btnNewID;
    private RadioButton btnPriceID;
    private RadioButton btnNumID;
    private RadioButton btnManID;
    private FrameLayout content;
    private RadioGroup btnGroup;
    private String keyword;
    private String gc_name;
    private FragmentManager manager;
    private GoodsListFragment goodsListFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_list_fragment_manager);
        manager = getSupportFragmentManager();
        keyword = getIntent().getStringExtra("keyword");
        gc_name = getIntent().getStringExtra("gc_name");
        initView();
    }

    private void initView() {
        btnGroup = (RadioGroup) findViewById(R.id.btnGroup);
        imageBack = (ImageView) findViewById(R.id.imageBack);
        textGoodsTabTitleName = (TextView) findViewById(R.id.textGoodsTabTitleName);
        btnNewID = (RadioButton) findViewById(R.id.btnNewID);
        btnPriceID = (RadioButton) findViewById(R.id.btnPriceID);
        btnNumID = (RadioButton) findViewById(R.id.btnNumID);
        btnManID = (RadioButton) findViewById(R.id.btnManID);
        content = (FrameLayout) findViewById(R.id.content);

        textGoodsTabTitleName.setText(gc_name);
        imageBack.setOnClickListener(this);
        btnGroup.setOnCheckedChangeListener(this);

        addFragmentIn(0);
    }

    @Override
    public void onClick(View v) {
        this.finish();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.btnNewID:
                addFragmentIn(0);
                break;
            case R.id.btnPriceID:
                addFragmentIn(1);
                break;
            case R.id.btnNumID:
                addFragmentIn(2);
                break;
            case R.id.btnManID:
                addFragmentIn(3);
                break;
        }
    }

    private void hideFragments(FragmentTransaction transaction) {
        if (goodsListFragment != null) {
            transaction.hide(goodsListFragment);
        }
    }

    private void addFragmentIn(int key) {
        FragmentTransaction transaction = manager.beginTransaction();
        hideFragments(transaction);
        if(goodsListFragment == null){
            goodsListFragment = new GoodsListFragment();
            goodsListFragment.key = key;
            goodsListFragment.keyword = keyword;
            transaction.add(R.id.content,goodsListFragment);
        }else{
            transaction.show(goodsListFragment);
            goodsListFragment.key = key;
            goodsListFragment.keyword = keyword;
            goodsListFragment.data.clear();
            goodsListFragment.loadingGoodsListData();
        }

        transaction.commitAllowingStateLoss();
    }
}
